apiVersion: v1
kind: Service
metadata:
  name: openpitrix-db
  namespace: ${NAMESPACE}
  labels:
    app: openpitrix
    tier: db
    version: ${VERSION}
spec:
  selector:
    app: openpitrix
    tier: db
  ports:
  - name: tcp
    protocol: TCP
    port: 3306
    targetPort: 3306

{% if persistence.enable is defined and persistence.enable == true %}     
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: openpitrix-db-pvc
  namespace: ${NAMESPACE}
  labels:
    app: openpitrix
    tier: db
spec:
  accessModes:
    - ReadWriteOnce
{% if persistence.storageClass is defined and persistence.storageClass != None %} 
  storageClassName: {{ persistence.storageClass }}
{% endif %}
  resources:
    requests:
      storage: 20Gi
{% endif %}

---
apiVersion: apps/v1beta2
kind: Deployment
metadata:
  name: openpitrix-db-deployment
  namespace: ${NAMESPACE}
  labels:
    app: openpitrix
    tier: db
    version: ${VERSION}
spec:
  selector:
    matchLabels:
      app: openpitrix
      tier: db
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: openpitrix
        tier: db
        version: ${VERSION}
    spec:
      containers:
      - name: openpitrix-db
        image: {{ mysql_repo }}:{{ mysql_tag }}
        imagePullPolicy: IfNotPresent
        lifecycle:
          postStart:
            exec:
              command: ["sh", "-c", "rm -rf /var/lib/mysql/lost+found"]
        args:
          - --default-authentication-plugin=mysql_native_password
          - --expire_logs_days=3
          - --binlog-expire-logs-seconds=604800
          - --max-binlog-size=1073741824
          - --max_allowed_packet=104857600
{% if is_allinone is not defined %}           
          - --max_connections=2000
{% endif %}
        env:
          # $ kubectl create secret generic mysql-pass --from-file=password.txt
          # make sure password.txt does not have a trailing newline
        - name: MYSQL_ROOT_PASSWORD
          valueFrom:
            secretKeyRef:
              key: password.txt
              name: mysql-pass
        - name: MYSQL_DATABASE
          value: kubesphere
        - name: MYSQL_ROOT_HOST
          value: "%"
        ports:
        - containerPort: 3306
          name: mysql
        volumeMounts:
        - name: db-persistent-storage
          mountPath: /var/lib/mysql
      tolerations:
        - key: node-role.kubernetes.io/master
          effect: NoSchedule
        - key: CriticalAddonsOnly
          operator: Exists
      affinity:
        nodeAffinity:
          preferredDuringSchedulingIgnoredDuringExecution:
          - weight: 100
            preference:
              matchExpressions:
              - key: node-role.kubernetes.io/master
                operator: In
                values:
                - "" 
      volumes:
      - name: db-persistent-storage
{% if persistence.enable is defined and persistence.enable == true %}  
        persistentVolumeClaim:
          claimName: openpitrix-db-pvc
{% else %} 
        hostPath:
          path: /var/lib/kubesphere/db-data
          type: DirectoryOrCreate
{% endif %}